package com.genexususa.eo.bing.data.wsSubclasses;

import com.genexususa.eo.bing.CampaignManagementServiceClasses.CampaignNegativeSites;
import com.genexususa.eo.bing.data.GxItemException;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class GxCampaignNegativeSites extends CampaignNegativeSites {
    private long gxCampaignId;

    public GxCampaignNegativeSites(Element element) throws IllegalArgumentException, GxItemException {
        if (element.getElementsByTagName("GxCampaignId").getLength() > 0 &&
            element.getElementsByTagName("GxCampaignId").item(0).getChildNodes().getLength() > 0 &&
            Long.valueOf(((Node)element.getElementsByTagName("GxCampaignId").item(0).getChildNodes().item(0)).getNodeValue()) != 0) {
            this.gxCampaignId = Long.valueOf(((Node)element.getElementsByTagName("GxCampaignId").item(0).getChildNodes().item(0)).getNodeValue());
        } else {
            throw new GxItemException("Missing external Campaign id");
        }
        if (element.getElementsByTagName("BingCampaignId").getLength() > 0 &&
            element.getElementsByTagName("BingCampaignId").item(0).getChildNodes().getLength() > 0 &&
            Long.valueOf(((Node)element.getElementsByTagName("BingCampaignId").item(0).getChildNodes().item(0)).getNodeValue()) != 0) {
            this.setCampaignId(Long.valueOf(((Node) element.getElementsByTagName("BingCampaignId").item(0).getChildNodes().item(0)).getNodeValue()));
        }
        NodeList negativeSitesNode = element.getElementsByTagName("NegativeSiteCollection");
        if (negativeSitesNode.getLength() > 0) {
            NodeList negativeSiteNode = ((Element)negativeSitesNode.item(0)).getElementsByTagName("NegativeSite");
            String[] negativeSites = new String[negativeSiteNode.getLength()];
            for (int i=0; i<negativeSiteNode.getLength(); i++) {
                if (negativeSiteNode.item(i).getChildNodes().getLength() > 0) {
                    negativeSites[i] = negativeSiteNode.item(i).getChildNodes().item(0).getNodeValue();
                }
            }
            this.setNegativeSites(negativeSites);
        }
    }

    public long getGxCampaignId() {
        return gxCampaignId;
    }

    public void setGxCampaignId(long gxCampaignId) {
        this.gxCampaignId = gxCampaignId;
    }
}
